package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hls.dao.GoodsCategoryDao;
import cn.hls.entity.GoodsCategory;
import cn.hls.entity.vo.PageInfo;
/**
 *@fileName: GoodsCategoryDaoImpl
 *@package: cn.christ.dao.impl
 *@Description: 场地数据操作接口实现类
 *@author: yinghui.zhang
 *@email: uuhui@163.com
 *@date: 2015-8-12
 */
@Component("goodsCategoryDao")
public class GoodsCategoryDaoImpl extends BaseDaoImpl<GoodsCategory> implements GoodsCategoryDao {

	@Override
	public PageInfo<GoodsCategory> loadGoodsCategoryInfo(PageInfo<GoodsCategory> page,
			GoodsCategory entity) throws Exception {
		StringBuilder selectHql=new StringBuilder("from GoodsCategory t where t.deleted=? ");
		StringBuilder countHql=new StringBuilder("select count(id) ");
		List<Object> params=new ArrayList<Object>();
		params.add(false);
		if(entity!=null){
			//按名称查询
			if(StringUtils.hasText(entity.getName())){
				selectHql.append("and t.name like ? ");
				params.add("%"+entity.getName().trim()+"%");
			}
			//按编码查询
			if(StringUtils.hasText(entity.getCode())){
				selectHql.append("and t.Code like ? ");
				params.add("%"+entity.getCode().trim()+"%");
			}
		}
		countHql.append(selectHql);
		return find(selectHql.toString(), countHql.toString(), params.toArray(), page);
	}

	@Override
	public GoodsCategory loadGoodsCategoryByCodeOrName( String code,String name)
			throws Exception {
		 String hql="from GoodsCategory where deleted=? and (code=? or  name=?) ";
		 Query query = getSession().createQuery(hql);
			query.setParameter(0, false);
			query.setParameter(1, code);
			query.setParameter(2, name);
			GoodsCategory entity = (GoodsCategory) query.uniqueResult();
			return entity;
	}

	@Override
	public List<GoodsCategory> loadGoodsCategoryInfo(GoodsCategory entity) throws Exception {
		StringBuilder selectHql=new StringBuilder("from GoodsCategory t where t.deleted=? ");
		List<Object> params=new ArrayList<Object>();
		params.add(false);
		if(entity!=null){
			//按名称查询
			if(StringUtils.hasText(entity.getName())){
				selectHql.append("and t.name like ? ");
				params.add("%"+entity.getName().trim()+"%");
			}
			//按国际名称查询
			if(StringUtils.hasText(entity.getInternatName())){
				selectHql.append("and t.internatName like ? ");
				params.add("%"+entity.getInternatName().trim()+"%");
			}
			//按编码查询
			if(StringUtils.hasText(entity.getCode())){
				selectHql.append("and t.Code like ? ");
				params.add("%"+entity.getCode().trim()+"%");
			}
			//按国际编码查询
			//按编码查询
			if(StringUtils.hasText(entity.getInternatCode())){
				selectHql.append("and t.internatCode like ? ");
				params.add("%"+entity.getInternatCode().trim()+"%");
			}
			 
		}
		return findByHql(selectHql.toString(), params.toArray());
	}
}
